package cn.miwu52.homeworkerp.Controller;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import cn.miwu52.homeworkerp.R;
import cn.miwu52.homeworkerp.dao.UserDao;

public class Register extends AppCompatActivity {
    private UserDao userDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        userDao = new UserDao(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        EditText userName = findViewById(R.id.userName);
        EditText passWord = findViewById(R.id.passWord);
        TextView isUserName = findViewById(R.id.isUserName);
        RadioGroup radioGroup  = findViewById(R.id.grade_group);
        Button reg_button = findViewById(R.id.reg_button);


        reg_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (reg_button.getText().toString().equals("注册")){
                    Integer radioId = radioGroup.getCheckedRadioButtonId();
                    if (userName.getText().toString().equals("")||passWord.getText().toString().equals("")){
                        Toast.makeText(Register.this,"请保证用户名或密码不为空",Toast.LENGTH_SHORT).show();
                    }else if (radioId == -1){
                        Toast.makeText(Register.this,"请选择用户权限",Toast.LENGTH_SHORT).show();
                    }else{
                        boolean b = userDao.insertUserName(userName.getText().toString(), passWord.getText().toString(), radioId == R.id.inOut_radio ? "出入库" : radioId == R.id.in_radio ? "入库" : "出库");
                        if (b){
                            Toast.makeText(Register.this,"注册成功！",Toast.LENGTH_SHORT).show();
                            reg_button.setText("立即登录");
                        }else{
                            Toast.makeText(Register.this,"注册失败！",Toast.LENGTH_SHORT).show();
                        }
                    }
                }else{
                    Intent intent = new Intent(Register.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });





        //判断当前用户名是否存在
        userName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {

                if (!b){
                    if (userDao.findUserByUserName(userName.getText().toString())){
                        isUserName.setText("当前用户名可用");
                        reg_button.setClickable(true);
                        isUserName.setTextColor(Color.rgb(0,255,0));
                    }else{
                        isUserName.setText("当前用户名已存在！");
                        reg_button.setClickable(false);
                        isUserName.setTextColor(Color.rgb(255,0,0));
                    }
                }
            }
        });
    }

}